package com.lyf.scm.plan.domain.repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.plan.api.dto.OperationLogRecordDTO;
import com.lyf.scm.plan.domain.convertor.OperationLogRecordConvertor;
import com.lyf.scm.plan.insfrastructure.db.dataobject.OperationLogRecordDO;
import com.lyf.scm.plan.insfrastructure.db.mapper.OperationLogRecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 操作日志记录表
 *
 * @author wangchlei
 * @date 2020/09/25
 */
@Service
public class OperationLogRecordRepository {
    @Resource
    private OperationLogRecordMapper operationLogRecordMapper;

    @Resource
    private OperationLogRecordConvertor operationLogRecordConvertor;

    /**
     * 新增大数据错误日志
     *
     * @param operationLogRecordDO
     * @return {@link Integer}
     **/
    public Integer addBigDataErrorLog(OperationLogRecordDO operationLogRecordDO) {
        return operationLogRecordMapper.addBigDataErrorLog(operationLogRecordDO);
    }

    /**
     * 批量新增大数据错误日志
     *
     * @param operationLogRecordDOs
     * @return {@link Integer}
     **/
    public void batchAddBigDataErrorLog(List<OperationLogRecordDO> operationLogRecordDOs) {
        operationLogRecordMapper.batchAddBigDataErrorLog(operationLogRecordDOs);
    }


    /**
     * 批量新增数据来源日志
     *
     * @param operationLogRecordDOs
     * @return {@link Integer}
     **/
    public void batchAddOperationLogRecord(List<OperationLogRecordDO> operationLogRecordDOs) {
        operationLogRecordMapper.batchAddOperationLogRecord(operationLogRecordDOs);
    }

    public PageInfo<OperationLogRecordDTO> getOperationLogs(OperationLogRecordDTO operationLogRecord) {
        //转换为do
        OperationLogRecordDO operationDo = operationLogRecordConvertor.dtoToDO(operationLogRecord);
        PageHelper.startPage(operationLogRecord.getPageNum(), operationLogRecord.getPageSize());
        return operationLogRecordConvertor.doToDTO(new PageInfo<>(operationLogRecordMapper.getOperationLogs(operationDo)));
    }

    public OperationLogRecordDTO insertOperationLog(OperationLogRecordDTO operationDto) {
        OperationLogRecordDO operationLogRecordDO = operationLogRecordConvertor.dtoToDO(operationDto);
        operationLogRecordMapper.insertOperationLog(operationLogRecordDO);
        return operationLogRecordConvertor.doToDTO(operationLogRecordDO);
    }

    /**
     * 编辑操作日志数据
     * @author wuyuanhang
     * @date 2020/11/16
     * @param operationDto 操作日志对象
     * @return int 更新条数
     */
    public int updateOperationLog(OperationLogRecordDTO operationDto) {
        OperationLogRecordDO operationLogRecordDO = operationLogRecordConvertor.dtoToDO(operationDto);
        return operationLogRecordMapper.updateOperationLog(operationLogRecordDO);
    }

    public void updateOperationLogStatus(OperationLogRecordDTO operationDto) {
        operationLogRecordMapper.updateOperationLogStatus(operationLogRecordConvertor.dtoToDO(operationDto));
    }


    /**
     * 拉取补救信息,时间在9点以后，23点以前
     *
     * @param startTime
     * @param endTime
     * @return {@link List<OperationLogRecordDO>}
     **/
    public List<OperationLogRecordDO> pullRemedialLog(Date startTime, Date endTime, Integer scope, Integer opStatus) {
        return operationLogRecordMapper.pullRemedialLog(startTime, endTime, scope, opStatus);
    }

    public Integer deleteBigDataErrorLog(Integer opSource, Date startTime, Date endTime, String opSign) {
        return operationLogRecordMapper.deleteBigDataErrorLog(opSource, startTime, endTime, opSign);
    }

    public List<OperationLogRecordDO> pullReasonableLog(Date startTime, Date endTime, Integer scope) {
        return operationLogRecordMapper.pullReasonableLog(startTime, endTime, scope);
    }

    public int updateInitiatedStatus(OperationLogRecordDO item) {
        return operationLogRecordMapper.updateInitiatedStatus(item);
    }

    public void updateToDel(String opSign){
        operationLogRecordMapper.updateToDel(opSign);
    }

    public int deleteById(Long id) {
        return operationLogRecordMapper.deleteById(id);
    }

    public void batchUpdateBigDataErrorLog(List<OperationLogRecordDO> operationLogRecordDOs, Date startTime, Date endTime) {
        operationLogRecordMapper.batchUpdateBigDataErrorLog(operationLogRecordDOs, startTime, endTime);
    }

    public void updateBigDataErrorLog(OperationLogRecordDO operationLogRecordDO, Date startTime, Date endTime) {
        operationLogRecordMapper.updateBigDataErrorLog(operationLogRecordDO, startTime, endTime);
    }

    public int updateStatusById(Long id) {
        return operationLogRecordMapper.updateStatusById(id);
    }

    public void batchUpdateOperationLogStatus(List<OperationLogRecordDTO> operationLogRecordDTOs) {
        operationLogRecordMapper.batchUpdateOperationLogStatus(operationLogRecordConvertor.dtoToDO(operationLogRecordDTOs));
    }
}
